<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<link type="text/css" rel="stylesheet" href="layui/css/layui.css"/>
		<script type="text/javascript" src="layui/layui.js"></script>
	</head>
	<body>
		<form id="addNews" class="layui-form" action="">
			<div class="layui-form-item">
				<label class="layui-form-label">
					<span style="color: red;">*</span>新闻标题</label>
				<div class="layui-input-inline">
					<input  type="text" id="newsTitle" name="newsTitle" placeholder="请输入新闻标题"
						lay-verify = "required" class="layui-input">
				</div>
			</div>
			
			 <div id="editor">
        <p>请输入新闻内容</p>
    </div>
    <textarea hidden="" required="required" id="newsContent" name="newsContent" style="width:100%; height:200px;"></textarea>
			
			<div class="layui-form-item">
				<label class="layui-form-label">
					<span style="color: red;">*</span>新闻分类</label>
				<div class="layui-input-inline">
					
					<select name="nid">
						<option value=" ">请选择分类</option>
						<c:forEach items="${newsTypeList }" var="newsType">
							<option value="${newsType.id }">${newsType.name }</option>
						</c:forEach>
						</select>
					
				</div>
			</div>
			
			<div class="layui-form-item">
				<label class="layui-form-label">
					<span style="color: red;">*</span>新闻是否在首页显示</label>
				<div class="layui-input-inline">
				          <input id="" type="radio" name="showHome" value="0" checked />否
				              &nbsp;&nbsp;&nbsp;&nbsp;
				          <input id="" type="radio" name="showHome" value="1" />是
				</div>
			</div>
			
			<div class="layui-form-item">
				<div class="layui-input-block">
				<button class="layui-btn" lay-submit lay-filter="createNews">提交</button>
				<button type="reset" class="layui-btn" lay-submit>取消</button>
				</div>
			</div>
		</form>
		<script type="text/javascript" src="js/wangEditor.min.js"></script>
		<script type="text/javascript" src="js/jquery-3.4.1.min.js"></script>
		<script type="text/javascript">
        var E = window.wangEditor;
        var editor = new E('#editor');
        // 或者 var editor = new E( document.getElementById('editor') )
        var $newsContent = $('#newsContent');
        editor.customConfig.onchange = function (html) {
            // 监控变化，同步更新到 textarea
            $newsContent.val(html);
        }
        editor.customConfig.uploadImgShowBase64 = true;
        editor.create();
        // 初始化 textarea 的值
        $newsContent.val(editor.txt.html());
        </script>
		<script>
			layui.use(['form','layer','upload'],function(){
				var form = layui.form,$ = layui.$,layer = layui.layer,upload = layui.upload;
				
				//执行实例
				  var uploadInst = upload.render({
				    elem: '#uploadBanner' //绑定元素
				    ,url: '../ImageServlet?req=upload' //上传接口
				    ,done: function(res){
				    	 //回显图片
				    	  layer.msg(res.message,{icon:6});
				    	  $("#imgFace").attr("src","../"+res.message);
				    	  //保存名字
				    	  $("#bannerImg").val(res.message);
				    }
				    ,error: function(){
				    }
				  });
				
				
				
				//23d66d84-fae1-45d5-bd07-b694662fe579
				form.on('submit(createNews)',function(data){
					//layer.msg("提交动作");
					// post把表单数据发送到服务器端
					// 使用序列化的时候 input的name属性当成参数名字  ，值是参数值
					var str = $("#addNews").serialize();
					$.post(
						"http://localhost:8080/fearless/NewsAdminServlet?req=newsAdd",
						str,
						function(res){
							if(res.success){
						   	 //先得到当前iframe层的索引
							var index = parent.layer.getFrameIndex(window.name); 
							//再执行关闭 
							parent.layer.close(index);  
							//关闭父级页面的表格
							parent.layui.table.reload('table');
						   }else{
						   	layer.alert("添加失败");
						   }
						},
						"json"
					)
					return false;// 停留在这里
				})
				
			})
			
		</script>
		
	</body>
</html>
